/*
 * Module 02
 *
 */

package ua.gl.basecamp.tabachnyy.testcase;

import java.util.List;

/**
 * @author yaroslav.tabachnyy@gmail.com
 * @version 1.02
 * @date 14.02.2013
 *
 */
public class RangeAnswerQuestion extends TypedAnswerQuestion {

    private static final double MAX_SCORE = 1.0;
    private static final double MIN_SCORE = 0.0;

    private double maxDeviation;

    public RangeAnswerQuestion(String stringRepresentation,
                               List<Answer> answers, double maxDeviation) {
        super(stringRepresentation, answers);
        this.maxDeviation = maxDeviation;
    }

    @Override
    public double getAnswersScore(List<UserAnswer> userAnswers) {
        final int ANSWER_INDEX = 0;

        if (userAnswers.size() != 1) {
            throw new IllegalArgumentException("Must be exactly 1 number!");
        }
        String userStringAnswer = userAnswers.get(ANSWER_INDEX).getStringRepresentation();
        String correctStringAnswer = getAnswer(ANSWER_INDEX).getStringRepresentation();

        double userAnswer = Double.parseDouble(userStringAnswer);
        double correctAnswer = Double.parseDouble(correctStringAnswer);

        return (Math.abs(userAnswer - correctAnswer) <= maxDeviation) ? MAX_SCORE
                                                                      : MIN_SCORE;
    }

    @Override
    public String toString() {
        return getStringRepresentation()
                + String.format("(max range - %.2f)\n", maxDeviation);
    }
}
